
const parseCookie = require('./cookie');
const log = require('./log');
const menus = require('./menus')
const admin_cookie = 'admin_cookie_test';
const user_cookie = 'user_cookie_test';

module.exports = {
    info:function(req,res){
        var cookie = parseCookie(req);
        if( cookie.JSESSIONID ){

            if( cookie.JSESSIONID == admin_cookie){
                res.status(200).json({
                    error_code: 0,
                    error_desc:'ok',
                    userName:'sysadmin',
                    roles:['ROLE_ADMIN','ROLE_USER'],
                    menus:menus.menus
                });
            }else{
                res.status(200).json({
                    error_code: 0,
                    error_desc:'ok',
                    userName:'sysuser',
                    roles:['ROLE_USER'],
                    menus:menus.menus
                });
            }            
        }else{
            res.status(200).json({
                error: 1
            });
        }
    },
    login:function(req,res){
        
        log(req);
        const { username,password } = req.body
        if( username === 'sysadmin' ){

            setTimeout(() => {
                res.status(200)
                .cookie('JSESSIONID',admin_cookie,{httpOnly:true})
                .json({
                    error_code: 0,
                    error_desc:'ok',
                    //roles:['ROLE_ADMIN','ROLE_USER'],
                    //menus:menus.menus
                });
            }, 1000);
        }else if( username === 'sysuser'){

            res.status(200)
            .cookie('JSESSIONID',user_cookie,{httpOnly:true})
            .json({
                error_code: 0,
                error_desc:'ok',
                //roles:['ROLE_USER'],
                //menus:menus.menus
            });
        }else{
            res.status(200).json({
                error_code: 100,
                error_desc:'用户名或者密码错误！',
            }).cookie('JSESSIONID','');
        }
    }
}